home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 8 / Night Owl CD-ROM (NOPV8) (Night Owl Publisher) (1993).ISO / 047a / lex_yacc.arj / EXPR.Y < prev    next >
Text File  |  1989-05-28  |  2KB  |  76 lines

  1.  
  2. /* Sample Yacc specification for simple desktop calculator; derived
  3.    from a specification in Aho et al: Compilers. Principles, Techniques
  4.    and Tools (sect. 4.9).
  5.    Lexical analyzer is described by Lex specification in exprlex.l.
  6.  
  7.    To compile parser and lexical analyzer, issue the following commands:
  8.      yacc expr
  9.      lex exprlex
  10.      tpc expr
  11.  
  12.    If you want to trace the actions executed by the parser, compile expr.pas
  13.    with the command:
  14.      tpc expr /Dyydebug
  15.    (In this case, it may be useful to refer to the parser description generated
  16.    by Yacc (file expr.lst).)
  17.  
  18.    Description: This program reads simple arithmetic expressions, constructed
  19.    with real numbers, operators +, -, *, /, unary - and parentheses (operators
  20.    have their usual precedence, unary minus is highest), from standard input
  21.    (one per line) and prints the result on standard output.
  22.    Variables are denoted by a single letter (no distinction between upper and
  23.    lowercase letters); they are assigned values through an assignment of the
  24.    form <var>=<expr>.
  25.    To terminate the program, type ^Z or a period `.' at the beginning of a
  26.    line. */
  27.  
  28. %{
  29. uses LexLib, YaccLib;
  30. {$I exprlex} { lexical analyser }
  31. var var_table : array [1..26] of real;
  32.   { variable table, initialized to zeros }
  33. %}
  34.  
  35. %union {
  36.   number : real;
  37.   variable : integer;
  38.   }
  39.  
  40. %token <number> NUMBER      /* real constants */
  41. %token <variable> VARIABLE    /* single letter variable */
  42. %type <number> expr             /* real-valued expressions */
  43. %left '+' '-'              /* operators */
  44. %left '*' '/'
  45. %right UMINUS
  46. %token EOF            /* . at the beginning of a line */
  47. %token ILLEGAL             /* illegal token */
  48.  
  49. %%
  50.  
  51. input    : /* empty */
  52.     | input '\n'         /* empty line */
  53.     | input expr '\n'     { writeln($2) }
  54.     | input VARIABLE '=' expr '\n'
  55.                  { var_table[$2] := $4 }
  56.         | input EOF          { yyaccept }
  57.     | error '\n'         { write('reenter last line: '); yyerrok }
  58.     ;
  59.  
  60. expr    :  expr '+' expr     { $$ := $1 + $3 }
  61.     |  expr '-' expr     { $$ := $1 - $3 }
  62.     |  expr '*' expr     { $$ := $1 * $3 }
  63.     |  expr '/' expr     { $$ := $1 / $3 }
  64.     |  '(' expr ')'         { $$ := $2 }
  65.     |  '-' expr %prec UMINUS { $$ := -$2 }
  66.     |  NUMBER         { $$ := $1 }
  67.         |  VARIABLE         { $$ := var_table[$1] }
  68.     ;
  69.  
  70. %%
  71.  
  72. var i : integer;
  73. begin
  74.   for i := 1 to 26 do var_table[i] := 0.0;
  75.   if yyparse=0 then { done }
  76. end.